package routePlanner.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

public class RouteDatabaseCreator {
	
	private final static Logger LOGGER = Logger.getLogger(RouteDatabaseCreator.class.getName()); 
	private static final String DRIVER = "org.sqlite.JDBC";
	private static final String DB_URL = "jdbc:sqlite:routePlanner.db";
	private static final String NO_JDBC = "There is no JDBC driver";
	private static final String NO_CONNECTION= "Cannot opern conection with JDBC";
	
	public void checkThatDriverExists() {
        try {
            Class.forName(RouteDatabaseCreator.DRIVER);
        } catch (ClassNotFoundException e) {
        	LOGGER.info(NO_JDBC);
        	e.printStackTrace();
        }
	}
	
	public Statement connectToDb() {
        try {
        	Connection connection = getConnection();
        	Statement statement = connection.createStatement();
        	return statement;
        } catch (SQLException e) {
        	LOGGER.info(NO_CONNECTION);
        }
        return null;
	}
	
	public Connection getConnection() {
		try {
			Class.forName(RouteDatabaseCreator.DRIVER);
			return DriverManager.getConnection(DB_URL);
		} catch (SQLException e) {
			new RuntimeException(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
